package com.wj.controller;

import com.wj.domain.vo.BookVO;
import com.wj.service.BookCacheService;
import com.wj.service.BookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description: 手动执行任务
 * @author: wj
 * @create: 2022/2/14 13:42
 */
@RestController
@Slf4j
@Api(tags = "小说手动任务管理器")
public class BookHandler {

    @Resource
    private BookService bookService;

    @Resource
    private BookCacheService bookCacheService;

    @GetMapping("/update/book")
    @ApiOperation("更新所有小说")
    public String updateFiction() throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();
        log.info("开始更新小说"+sdf.format(now));
        bookService.updateNewBook();
        Date date = new Date();
        log.info("更新完成，"+sdf.format(date));
        return "执行完成";
    }

    @GetMapping("/create/book")
    @ApiOperation("生成需要缓存的小说")
    public String createBookFile(){
        //获取所有小说
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
        bookCacheService.createNewBookFile();
        return sdf.format(new Date())+"缓存执行完成";
    }

    @GetMapping("/completionBookContent")
    @ApiOperation("补全小说未完成章节内容")
    public String completionBookContent(String bookId){
        // 补全小说未完成章节内容
        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
        bookService.completionBookContent(bookId);
        return sdf.format(new Date())+"补全内容执行完成";
    }

    @GetMapping("/getNoChapterContentBook")
    @ApiOperation("获取未拉取完成的小说")
    public List<BookVO> getNoChapterContentBook(){
        List<BookVO> list = bookService.getNoChapterContentBook();
        return list;
    }
}